package com.zh.api.controller.operation;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.operation.domain.BedDO;
import com.zh.operation.service.BedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@Api("午休【床位管理】")
public class BedController extends BaseController {
	@Autowired
	private BedService bedService;

	@Log("所有床位信息【包含分页条件】")
	@ResponseBody
	@RequestMapping(value = "listOperationBed", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOpercationBed(@RequestBody Map<String, Object> params){
	    if(CheckEmptyUtil.isEmpty(params)){
	        return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
		//查询列表数据
        Query query = new Query(params);
		List<BedDO> bedList = bedService.list(query);
		int total = bedService.count(query);
		PageUtils pageUtils = new PageUtils(bedList, total);
		return ResultVO.success(pageUtils);
	}

    @Log("编辑床位信息")
    @ResponseBody
    @RequestMapping(value = "editOperationBedById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResultVO editOperationBedById(@RequestBody BedDO bed){
	    if(bed==null || CheckEmptyUtil.isEmpty(bed.getBedId())){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
        BedDO bedDO = bedService.get(bed.getBedId());
        return ResultVO.success(bedDO);
	}

    @Log("新增床位信息")
    @ResponseBody
    @RequestMapping(value = "saveOperationBed", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationBed(@RequestBody BedDO bed){
        if(bed==null){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
		bed.setUpdateBy(getUserId());//创建热ID
		if(bedService.save(bed)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

    @Log("更新床位信息")
    @ResponseBody
    @RequestMapping(value = "updateOperationBedById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
   public ResultVO updateOperationBedById(@RequestBody BedDO bed){
        if(bed==null || CheckEmptyUtil.isEmpty(bed.getBedId())){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
		bedService.update(bed);
		return ResultVO.success();
	}

    @Log("删除床位信息")
    @ResponseBody
    @RequestMapping(value = "removeOperationBedById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResultVO removeOperationBedById(@RequestBody BedDO bed){
        if(bed==null || CheckEmptyUtil.isEmpty(bed.getBedId())){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
		if(bedService.remove(bed.getBedId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
    @Log("查询所有床位信息")
    @ResponseBody
    @RequestMapping(value = "getAllOperationBed", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO getAll(){
		return  ResultVO.success(bedService.getAll());
	}


//	@Log("设备绑定功能")
//	@ResponseBody
//	@RequestMapping(value = "getAllOperationBed", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//	public ResultVO fun(@PathVariable("bedId") String bedId){
//		BedDO bedDao=new BedDO();
//		bedDao.setBedId(bedId);
//       return ResultVO.success(bedDao);
//	}
//
//	@Log("更新床位")
//	@ResponseBody
//	@RequestMapping(value = "updateOperationBed", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//    ResultVO updateBed( BedDO bed){
//		bedService.update(bed);
//		return ResultVO.success();
//	}
//
//	@RequestMapping("/deleteBed/{bedId}")
//    @ApiOperation("解除床位设备")
//    @ResponseBody
//    ResultVO deleteBed(@PathVariable("bedId") String bedId){
//		BedDO bedDO = bedService.get(bedId);
//		return ResultVO.success(bedDO);
//	}
    @RequestMapping(value = "batchRemoveOperationBedByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation("批量删除床位信息")
    public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
        if(jsonParam==null){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
        List<String> ids= (List<String>)jsonParam.get("ids");
        if(CheckEmptyUtil.isEmpty(ids)){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
        String[] strs1=ids.toArray(new String[ids.size()]);
        if(bedService.batchRemove(strs1)>0){
            return ResultVO.success();
        }
        return ResultVO.error();
    }
}
